/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bitcar.platform.person.service;

import com.bitcar.common.hibernate.HibernateDao;
import com.bitcar.platform.person.model.DailyReport;
import com.bitcar.platform.person.model.ReportDetail;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.dozer.DozerBeanMapper;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author shd
 */
@Service
@Transactional
public class PersonService {
    @Resource
    private HibernateDao dao;
   
    private DozerBeanMapper mapper;
    
    public void saveDailyReport(DailyReport dailyReport){
        this.dao.save(dailyReport);
    }
    
    /**
     * relationid  上次关联的detail.id  查看从始至终的一个事件的逐渐变化    
     * firstid  该条工作内容对应的立项第一条detail.id。查看一个事件的所有记录内容
     * reportid is not null
     * @param detail 
     */
    public void saveReportDetail(ReportDetail detail){        
        this.dao.save(detail);
        if(detail.getFirstid()==null){
            detail.setFirstid(detail.getId());
            this.dao.update(detail);
        }
        if(detail.getRelationid()==null){
            detail.setRelationid(detail.getId());
            this.dao.update(detail);
        }
    }
    
    public void updateReportDetail(ReportDetail detail){
        ReportDetail src= (ReportDetail)this.dao.get(ReportDetail.class, detail.getId());
        mapper.map(detail, src);
        
        this.dao.update(src);
    }
    
    public DailyReport getDailyReport(long id){
        return (DailyReport)this.dao.get(DailyReport.class, id);
    }
    
    public List<DailyReport> getDailyReport(long id,Date starttime,Date endtime){
        List drlist= this.dao.list("from DailyReport dr where dr.userid=? and reportdate between ? and ?", new Object[]{id,starttime,endtime});
        return drlist;
    }
    
    
    public List<ReportDetail> getDailyReportByUserid(int userid,Date starttime,Date endtime){
       List<ReportDetail> details=this.dao.list("from ReportDetail rd where rd.reportid in(select id from DailyReport where userid=?"
                + " and reportdate between ? and ? ) order by rd.start,id", new Object[]{userid,starttime,endtime==null?new Date():endtime});
       
       return details;
    }
    
   
    
    
    public List<ReportDetail> getReportDetailsByReport(int reportid){
        return (List<ReportDetail>)this.dao.list(
                "from ReportDetail where reportid=? ", new Object[]{reportid});
    }
    
    public List<ReportDetail> getReportDetailsByFirst(long firstid){
        return (List<ReportDetail>)this.dao.list("from ReportDetail where firstid=?", new Object[]{firstid});
    }

    public HibernateDao getDao() {
        return dao;
    }

    public void setDao(HibernateDao dao) {
        this.dao = dao;
    }

    public DozerBeanMapper getMapper() {
        return mapper;
    }

    
    @Resource
    @Required
    public void setMapper(DozerBeanMapper mapper) {
        this.mapper = mapper;
    }


}
